<template>
  <div class="mod-config">
    <el-form :inline="true" :model="dataForm" @keyup.enter.native="getDataList()">
      <el-select v-model="label" placeholder="请选择焊机" @change="selectProductType">
        <el-option
          v-for="item in options"
          :key="item.value"
          :label="item.label"
          :value="{ value: item.value, label: item.label}">
        </el-option>
      </el-select>
<!--      <el-form-item>-->
<!--        <el-input v-model="dataForm.key" placeholder="焊机名称" clearable></el-input>-->
<!--      </el-form-item>-->
      <el-date-picker
        v-model="value1"
        value-format="yyyy-MM-dd"
        type="daterange"
        range-separator="至"
        start-placeholder="开始日期"
        end-placeholder="结束日期">
      </el-date-picker>
      <el-form-item>
        <el-button @click="getwminfo()">查询</el-button>
<!--        <el-button v-if="isAuth('wmunnormal:wmunnormal:save')" type="primary" @click="addOrUpdateHandle()">新增</el-button>-->
        <el-button v-if="isAuth('wmunnormal:wmunnormal:delete')" type="danger" @click="deleteHandle()" :disabled="dataListSelections.length <= 0">批量删除</el-button>
        <el-button type="success" @click="submit" :disabled="disabled">导出</el-button>
      </el-form-item>
    </el-form>
    <el-table
      :data="dataList"
      border
      v-loading="dataListLoading"
      @selection-change="selectionChangeHandle"
      style="width: 100%;">
      <el-table-column
        type="selection"
        header-align="center"
        align="center"
        width="50">
      </el-table-column>
      <el-table-column
        prop="wmid"
        header-align="center"
        align="center"
        label="焊机编号">
      </el-table-column>
      <el-table-column
        prop="wmname"
        header-align="center"
        align="center"
        label="焊机名称">
      </el-table-column>
      <el-table-column
        prop="wmgroup"
        header-align="center"
        align="center"
        label="班组名称">
      </el-table-column>
      <el-table-column
        prop="curunnormal"
        header-align="center"
        align="center"
        label="电流超规范">
      </el-table-column>
      <el-table-column
        prop="volunnormal"
        header-align="center"
        align="center"
        label="电压超规范">
      </el-table-column>
      <el-table-column
        prop="pgmunnormal"
        header-align="center"
        align="center"
        label="焊丝超规范">
      </el-table-column>
      <el-table-column
        prop="remunnormal"
        header-align="center"
        align="center"
        label="气体超规范">
      </el-table-column>
      <el-table-column
        prop="allunnormal"
        header-align="center"
        align="center"
        label="总超规范">
      </el-table-column>
      <el-table-column
        prop="wmtime"
        header-align="center"
        align="center"
        label="时间">
      </el-table-column>
      <el-table-column
        fixed="right"
        header-align="center"
        align="center"
        width="150"
        label="操作">
        <template slot-scope="scope">
          <el-button type="text" size="small" @click="addOrUpdateHandle(scope.row.wmid)">修改</el-button>
          <el-button type="text" size="small" @click="deleteHandle(scope.row.wmid)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      @size-change="sizeChangeHandle"
      @current-change="currentChangeHandle"
      :current-page="pageIndex"
      :page-sizes="[10, 20, 50, 100]"
      :page-size="pageSize"
      :total="totalPage"
      layout="total, sizes, prev, pager, next, jumper">
    </el-pagination>
    <!-- 弹窗, 新增 / 修改 -->
    <add-or-update v-if="addOrUpdateVisible" ref="addOrUpdate" @refreshDataList="getDataList"></add-or-update>
  </div>
</template>

<script>
  import AddOrUpdate from './wmunnormal-add-or-update'
  import xlsx from 'xlsx'
  import {Loading} from 'element-ui'
  export default {
    data () {
      return {
        dataForm: {
          key: ''
        },
        options: [{
          value: '选项1',
          label: 'root'
        }, {
          value: '选项2',
          label: '1号焊机'
        }, {
          value: '选项3',
          label: '2号焊机'
        }, {
          value: '选项4',
          label: '3号焊机'
        }, {
          value: '选项5',
          label: '4号焊机'
        }],
        value: '',
        value1: '',
        label: '',
        dataList: [],
        pageIndex: 1,
        pageSize: 10,
        totalPage: 0,
        disabled: false,
        loading: false,
        dataListLoading: false,
        dataListSelections: [],
        addOrUpdateVisible: false
      }
    },
    components: {
      AddOrUpdate
    },
    activated () {
      this.getDataList()
    },
    methods: {
      getwminfo(){
        console.log("焊机："+this.label)
        console.log("开始时间："+this.value1[0])
        console.log("结束时间："+this.value1[1])
        this.dataListLoading = true
        this.$http({
          url: this.$http.adornUrl('/wmunnormal/wmunnormal/getWm'),
          method: 'get',
          params: this.$http.adornParams({
            'wmName': this.label,
            'startTime': this.value1[0],
            'endTime': this.value1[1]
          })
        }).then(({data}) => {
          if (data) {
            console.log(data)
            this.dataList = data
          } else {
            this.dataList = []
          }
          this.dataListLoading = false
        })
      },
      // 获取选择器的内容
      selectWeldingMachine(){
        console.log(this.label)
        //console.log(this.value1)
      },
      selectProductType(data){
        // 将data对象解构
        const { value, label } = data;
        this.label = label;
        this.value = value;
      },
      // 导出操作
      submit(){
        if(this.dataListSelections.length <= 0){
          this.$message({
            message: "请选择需要导出的数据!",
            type: 'warning',
            showClose: true
          });
          return;
        }
        this.disabled = true;
        let loadingInstance = Loading.service({
          text: "正在导出...",
          background: "rgba(0,0,0,.5)"
        });
        let arr = this.dataListSelections.map(item => {
          console.log(item)
          return {
            NO: item.no,
            厂家名称: item.cncname,
            设备名称: item.cpyname,
            设备类型: item.cnctype,
            工作状态: item.status,
            焊接电流: item.welcur,
            焊接电压: item.welvol,
            内部温度: item.temperature,
            内部湿度: item.tumidity,
            其流量: item.rempos,
            送丝速度: item.curpgm
          };
        });
        //console.log(arr)
        let sheet = xlsx.utils.json_to_sheet(arr);
        const book1 = xlsx.utils.book_new();
        xlsx.utils.book_append_sheet(book1,sheet,"sheet1");
        xlsx.writeFile(book1,`user${new Date().getTime()}.xls`);
        loadingInstance.close();
        this.disabled = false;
      },
      // 获取数据列表
      getDataList () {
        this.dataListLoading = true
        this.$http({
          url: this.$http.adornUrl('/wmunnormal/wmunnormal/list'),
          method: 'get',
          params: this.$http.adornParams({
            'page': this.pageIndex,
            'limit': this.pageSize,
            'key': this.dataForm.key
          })
        }).then(({data}) => {
          if (data && data.code === 0) {
            this.dataList = data.page.list
            this.totalPage = data.page.totalCount
          } else {
            this.dataList = []
            this.totalPage = 0
          }
          this.dataListLoading = false
        })
      },
      // 每页数
      sizeChangeHandle (val) {
        this.pageSize = val
        this.pageIndex = 1
        this.getDataList()
      },
      // 当前页
      currentChangeHandle (val) {
        this.pageIndex = val
        this.getDataList()
      },
      // 多选
      selectionChangeHandle (val) {
        this.dataListSelections = val
      },
      // 新增 / 修改
      addOrUpdateHandle (id) {
        this.addOrUpdateVisible = true
        this.$nextTick(() => {
          this.$refs.addOrUpdate.init(id)
        })
      },
      // 删除
      deleteHandle (id) {
        var ids = id ? [id] : this.dataListSelections.map(item => {
          return item.wmid
        })
        this.$confirm(`确定对[id=${ids.join(',')}]进行[${id ? '删除' : '批量删除'}]操作?`, '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          this.$http({
            url: this.$http.adornUrl('/wmunnormal/wmunnormal/delete'),
            method: 'post',
            data: this.$http.adornData(ids, false)
          }).then(({data}) => {
            if (data && data.code === 0) {
              this.$message({
                message: '操作成功',
                type: 'success',
                duration: 1500,
                onClose: () => {
                  this.getDataList()
                }
              })
            } else {
              this.$message.error(data.msg)
            }
          })
        })
      }
    }
  }
</script>
